package development.bean;

import java.util.Vector;

import state.util.Hashtable;

public class ChatRoom {
  
  private String name = "";
  
  private String title = "";
  
  private boolean hasPassword;
  
  private String roomMaster = "";
  
  private int concurrentUser = 0;
  
  private int maxUser = Integer.MAX_VALUE;
  
  private Vector chatMessages = new Vector();
  
  private Hashtable members = new Hashtable(); // member's username
  
  public String getName() {
    return name;
  }

  public ChatRoom setName(String name1) {
    this.name = name1;
    return this;
  }

  public String getTitle() {
    return title;
  }

  public ChatRoom setTitle(String title1) {
    this.title = title1;
    return this;
  }
  
  public boolean isHasPassword() {
    return hasPassword;
  }

  public String getRoomMaster() {
    return roomMaster;
  }

  public ChatRoom setRoomMaster(String roomMaster1) {
    this.roomMaster = roomMaster1;
    return this;
  }
  
  public int getMaxUser() {
    return maxUser;
  }

  public ChatRoom setMaxUser(int maxUser1) {
    this.maxUser = maxUser1;
    return this;
  }
  
  public int getConcurrentUser() {
    return concurrentUser;
  }

  public ChatRoom setConcurrentUser(int concurrentUser1) {
    this.concurrentUser = concurrentUser1;
    return this;
  }

  public ChatRoom setHasPassword(boolean hasPassword1) {
    this.hasPassword = hasPassword1;
    return this;
  }
  
  public ChatRoom putMessage(ChatMessage chatMessage) {
    if (chatMessages.size() > 0) {
      ChatMessage nearestMessage = (ChatMessage) chatMessages.elementAt(chatMessages.size() - 1);
      if (nearestMessage.getFromUser().equals(chatMessage.getFromUser())) {
        nearestMessage.appendMessage("\n").appendMessage(chatMessage.getMessage());
      } else {
        chatMessages.addElement(chatMessage);
      }
    } else {
      chatMessages.addElement(chatMessage);
    }
    return this;
  }
  
  public Vector getChatMessages() {
    return chatMessages;
  }
  
  public void putMember(String user) {
    members.put(user, user);
  }
  
  public void removeMember(String user) {
    members.remove(user);
  }
  
  public void removeAllMembers() {
    members.removeAll();
  }
  
  public Hashtable getMembers() {
    return members;
  }

  public String toString() {
    StringBuffer builder = new StringBuffer();
    builder.append("ChatRoom[");
    builder.append(" name=\"" + name + "\", ");
    builder.append(" title=\"" + title + "\", ");
    builder.append(" roomMaster=\"" + ((roomMaster == null) ? null : roomMaster) + "\", ");
    builder.append(" maxUser=" + maxUser + "");
    return builder.toString();
  }
}
